#!/usr/bin/env ruby

# Copyright © 2021 sev, p
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation, either version 3 of the License, or (at
# your option) any later version.
#
# This program is distributed in the hope that it will be useful, but
# WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this program.  If not, see <https://www.gnu.org/licenses/>.

%w(
	json
	openssl
	net/http
	uri
).each &method(:require)

ARGV.each { |host|
	data = JSON.parse(Net::HTTP.get(URI("https://#{host}/api/v1/instance")))
	admin = begin
		"#{data['contact_account']['url']}"
	rescue NoMethodError
	end

	if !admin
		begin
			suppl = JSON.parse(Net::HTTP.get(URI("https://#{host}/nodeinfo/2.0.json")))
			admin = suppl.dig('metadata', 'staffAccounts').inspect
		rescue
		end
	end

	puts "- #{data['uri']} — #{(data['title'] || data['description']).inspect}",
		"\temail: #{data['email']}",
		"\tadmin: #{admin}",
		"\tstats: #{data['stats'].to_json}",
		''
}
